package com.hz.common.view;

import static com.hz.common.utils.SizeUtils.dp2px;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import io.gitee.hongzhenw.R;


/**
 * Created by kevin on 2023/1/9
 */
public class HwAlertDialog extends AlertDialog {

    private EditText mEditTextContent;
    private TextView mTextViewContent;
    private IHwAlertDialog mIHwAlertDialog;
    private Button mCancelTextView, mOkTextView;

    public void setIHwAlertDialogListener(IHwAlertDialog alertDialog) {
        mIHwAlertDialog = alertDialog;
    }

    public HwAlertDialog(Context context, boolean isTip, int gravity) {
        super(context);

        initView(context, isTip, gravity);
    }

    public HwAlertDialog setCancelText(String msg) {
        if (mCancelTextView != null) {
            mCancelTextView.setText(msg);
        }
        return this;
    }

    public HwAlertDialog setOkText(String msg) {
        if (mOkTextView != null) {
            mOkTextView.setText(msg);
        }
        return this;
    }

    public HwAlertDialog setOkTextColor(int color) {
        if (mOkTextView != null) {
            mOkTextView.setTextColor(color);
        }
        return this;
    }

    public HwAlertDialog setCancelTextColor(int color) {
        if (mCancelTextView != null) {
            mCancelTextView.setTextColor(color);
        }
        return this;
    }

    public void setEditTextHint(String hint) {
        if (mEditTextContent != null) {
            mEditTextContent.setHint(hint);
        }
    }

    private TextView createTextViewTip(Context ctx) {
        TextView textView = new TextView(ctx);
        textView.setText("温馨提示");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
        textView.setTextColor(ctx.getResources().getColor(R.color.black));
        return textView;
    }

    private TextView createTextViewContent(Context ctx) {
        TextView textView = new TextView(ctx);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-1, -2);
        params.setMargins(0, dp2px(25), 0, dp2px(25));
        textView.setLayoutParams(params);
        textView.setTextColor(ctx.getResources().getColor(R.color.black));
        return textView;
    }

    private EditText createEditTextContent(Context ctx) {
        EditText editText = new EditText(ctx);
        editText.setHint("请输入文本");
        editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
        editText.setBackgroundResource(R.drawable.h_bg_edit_input);
        editText.setPadding(dp2px(8), 0, dp2px(8), 0);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-1, dp2px(40));
        params.setMargins(0, dp2px(30), 0, dp2px(15));
        editText.setLayoutParams(params);
        editText.setTextColor(ctx.getResources().getColor(R.color.black));
        return editText;
    }

    @Override
    public void show() {
        super.show();
        // 自动弹出键盘
    }

    private Button createTextViewButton(Context ctx) {
        Button textView = new Button(ctx);
        textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        textView.setBackgroundResource(R.drawable.h_selector_button);
        textView.setTextColor(ctx.getResources().getColor(R.color.black));
        textView.setStateListAnimator(null);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, -2, 1);
        int dp5 = dp2px(5);
        params.setMargins(dp5, dp5, dp5, dp5);
        textView.setLayoutParams(params);
        return textView;
    }

    private View createDivider(Context ctx) {
        View view = new View(ctx);
        view.setLayoutParams(new LinearLayout.LayoutParams(dp2px(1), dp2px(25)));
        view.setBackgroundColor(ctx.getResources().getColor(R.color.dialog_line));
        return view;
    }

    private void initView(Context ctx, boolean isTip, int gravity) {
        Window window = getWindow();
        window.setBackgroundDrawable(new ColorDrawable());
        // 底部显示
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = gravity;
        // 整体布局
        LinearLayout layout = new LinearLayout(ctx);
        int dp10 = dp2px(10);
        layout.setPadding(dp10, dp2px(20), dp10, dp10);
        layout.setBackgroundResource(R.drawable.h_bg_shape_radius_25);
        layout.setOrientation(LinearLayout.VERTICAL);
        // 温馨提示
        TextView tip = createTextViewTip(ctx);
        layout.addView(tip);
        if (isTip) {
            // 提示内容
            mTextViewContent = createTextViewContent(ctx);
            layout.addView(mTextViewContent);
        } else {
            // 输入内容
            mEditTextContent = createEditTextContent(ctx);
            layout.addView(mEditTextContent);
        }
        // 底部按钮布局
        LinearLayout bottom = new LinearLayout(ctx);
        bottom.setGravity(Gravity.CENTER_VERTICAL);
        bottom.setOrientation(LinearLayout.HORIZONTAL);
        // 取消按钮
        mCancelTextView = createTextViewButton(ctx);
        mCancelTextView.setText("取消");
        bottom.addView(mCancelTextView);
        // 分割线
        View divider = createDivider(ctx);
        bottom.addView(divider);
        // 确定按钮
        mOkTextView = createTextViewButton(ctx);
        mOkTextView.setText("确定");
        bottom.addView(mOkTextView);
        // 取消事件
        mCancelTextView.setOnClickListener(v -> {
            if (mIHwAlertDialog != null) {
                mIHwAlertDialog.cancelClick();
            }
            dismiss();
        });
        // 确定事件
        mOkTextView.setOnClickListener(v -> {
            String title = mEditTextContent == null ? null : mEditTextContent.getText().toString().trim();
            if (mIHwAlertDialog != null) {
                if (mEditTextContent != null) {
                    if (TextUtils.isEmpty(title)) {
                        // 输入框不能为空
                        mEditTextContent.setText("");
                        mEditTextContent.setHint("请输入文本");
                        Animation shake = AnimationUtils.loadAnimation(ctx, R.anim.hshake);
                        mEditTextContent.startAnimation(shake);
                        return;
                    }
                }
                mIHwAlertDialog.finishClick(title);
            }
            dismiss();
        });
        layout.addView(bottom);
        setView(layout);
    }

    @Override
    public void setMessage(CharSequence message) {
        if (mTextViewContent != null) {
            mTextViewContent.setText(message);
        }
        if (mEditTextContent != null) {
            mEditTextContent.setText(message);
            mEditTextContent.setSelection(TextUtils.isEmpty(message) ? 0 : message.length());
        }
    }

    public interface IHwAlertDialog {
        void cancelClick();

        void finishClick(String msg);
    }
}
